Primary and Foreign Key
Keys define how data is uniquely identified and related inside a database. For automation testers, understanding keys is critical to:
- Fetch the right records
- Validate relationships
- Avoid false test failures
What is a Primary Key?β
A Primary Key (PK) uniquely identifies each row in a table.
Exampleβ
USERS
--------------------------------
| user_id (PK) | username | ... |
--------------------------------
| 101 | john | |
| 102 | mary | |
Rules of Primary Keyβ
- Must be unique
- Cannot be NULL
- One primary key per table (can be composite)
Why Primary Key Matters for Testersβ
- Fetch exact records reliably
- Avoid validating duplicate rows
- Ensure assertions target the correct data
Bad validation β
βCheck first row in USERS tableβ
Good validation β
βFetch row where user_id = 101β
What is a Foreign Key?β
A Foreign Key (FK) creates a relationship between two tables.
Exampleβ
ORDERS
------------------------------------
| order_id (PK) | user_id (FK) | ...|
------------------------------------
| 5001 | 101 | |
Here:
ORDERS.user_idβ refers toUSERS.user_id
This enforces referential integrity.
Why Foreign Key Matters for Automationβ
- Validates data across tables
- Ensures business rules are followed
- Enables joins for backend verification
Real Scenarioβ
- User places an order
- Order must reference an existing user
Automation check:
- Order exists
- Linked user exists
- Relationship is valid
Primary Key vs Foreign Key (Quick Comparison)β
| Aspect | Primary Key | Foreign Key |
|---|---|---|
| Purpose | Identify a row | Link tables |
| Uniqueness | Must be unique | Can repeat |
| NULL allowed | β No | β Yes (sometimes) |
| Used by testers | Always | Very often |
Composite Keys (Awareness)β
A composite key uses multiple columns as a primary key.
Example:
(order_id, product_id)
Tester note:
- Rarely validated directly
- Be aware when querying
Common Automation Mistakes ββ
- Ignoring primary keys in validations
- Using non-unique columns (like username)
- Assuming FK always exists
- Hardcoding IDs without context
Best Practices (Tester-Level)β
- Always identify the primary key first
- Use PKs in WHERE clauses
- Understand FK relationships before joins
- Validate relationships, not just data
Key Takeawaysβ
- Primary key uniquely identifies a record
- Foreign key links related tables
- Keys make validations accurate
- Essential before learning joins & SQL